package StudentSystemUpgrade;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-09-23
 * Time: 09:46
 */
public class StudentSystem {
    public static void startStudentSystem() {
        ArrayList<Student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("--------------欢迎来到学生管理系统-----------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");

            System.out.println("请输入您的选择：");
            Scanner scanner = new Scanner(System.in);
            String ret = scanner.next();
            switch (ret){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("退出");
                    //System.exit(0);
                    //方式2：使用标号，可以指定break到特定地方跳出
                    break loop;
                default:
                    System.out.println("没有这个选项");
            }
        }
    }


    //1.添加学生
    public static void addStudent(ArrayList<Student> list){
        //System.out.println("添加学生");
        Student student = new Student();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请输入学生的id:");
            String id = scanner.next();
            boolean ret = checkId(list,id);
            if(ret){
                //表示id存在
                System.out.println("学生id已经存在，请重新录入");
            }else {
                student.setId(id);
                break;
            }
        }

        System.out.println("请输入学生的姓名:");
        String name = scanner.next();
        student.setName(name);

        System.out.println("请输入学生的年龄:");
        int age = scanner.nextInt();
        student.setAge(age);

        System.out.println("请输入学生的家庭地址");
        String address = scanner.next();
        student.setAddress(address);

        //添加学生对象到集合中
        list.add(student);

        //提示用户
        System.out.println("学生信息添加成功");
    }

    //判断id在集合中是否存在
    public static boolean checkId(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getId().equals(id)){
                return true;
            }
        }
        return false;
    }
    //通过id获取索引的方法
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
    //2.删除学生
    public static void deleteStudent(ArrayList<Student> list){
        //System.out.println("删除学生");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生id:");
        String id = scanner.next();
        //查询id在集合中的索引
        int index = getIndex(list,id);
        if(index >= 0){//判断id在集合中是否存在
            list.remove(index);
            System.out.println("id为" + id + "的学生删除成功");
        }else {
            System.out.println("id不存在，删除失败");
        }
    }
    //3.修改学生
    public static void updateStudent(ArrayList<Student> list){
        //System.out.println("修改学生");
        Scanner scanner = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入要修改的学生id:");
        String id = scanner.next();

        boolean ret = checkId(list,id);

        if(ret){
            System.out.println("请输入新的学生id");
            String newId = scanner.next();
            stu.setId(newId);
            System.out.println("请输入新的学生姓名");
            String newName = scanner.next();
            stu.setName(newName);
            System.out.println("请输入新的学生年龄");
            int newAge = scanner.nextInt();
            stu.setAge(newAge);
            System.out.println("请输入新的学生家庭地址");
            String newAddress = scanner.next();
            stu.setAddress(newAddress);

            //获取修改学生的集合索引
            int index = getIndex(list,id);
            list.set(index,stu);

            System.out.println("修改id为" + id +"的信息成功");

        }else {
            System.out.println("修改的学生不存在");
        }
    }
    //4.查询学生
    public static void queryStudent(ArrayList<Student> list){
        //System.out.println("查询学生");
        if(list.size() == 0){
            System.out.println("当前无学生信息，请添加后在查询");
            return;
        }
        //打印表头信息
        System.out.println("id\t\t姓名\t\t年龄\t\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" +stu.getAddress());
        }
    }
}
